package com.jy.adscm.wechat.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.WechatRepairPicture;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.bean.WechatUserRole;
import com.jy.adscm.wechat.service.WechatRepairPictureService;
import com.jy.adscm.wechat.service.WechatUserService;
import com.jy.adscm.wechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-09-25
 */
@RestController
@RequestMapping("adscm/wechat/wechatRepairPicture")
public class WechatRepairPictureController {
    @Autowired
    WechatRepairPictureService wechatRepairPictureService;
    @Value("${wechat.upload}")
    private  String wechatUploadPath;
    @Value("${wechat.http}")
    private  String wechatUploadUrl;
    @Autowired
    WechatUserService wechatUserService;
    @Autowired
    private RedisOperator redis;
    @RequestMapping("upload")
    public SimpleCoreResponse uploadSjAttachment(HttpServletRequest request) throws AdscmSystemException, IOException {
        String sessionKey = request.getParameter("sessionKey");
        String s = redis.get(sessionKey);
        WechatUser wechatUser = wechatUserService.selectByOpenId(s);
       if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
           throw new AdscmSystemException("未注册");
       }
        List<String> paths = null;
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        if(multipartResolver.isMultipart(request)){
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
            String path = wechatUploadPath +new SimpleDateFormat("yyyyMM").format(new Date()) + File.separator + UUID.randomUUID().toString().replaceAll("-","") + File.separator;
            paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest,path);
        }
        String result="";
        if (paths!=null){
             result= wechatUploadUrl+paths.get(0).replace(wechatUploadPath,"");
        }
        return new SimpleCoreResponse("200","上传成功",result);
    }

    @RequestMapping("delete")
    public SimpleCoreResponse delete( String path, HttpServletRequest request) throws AdscmSystemException, IOException {
        String sessionKey = request.getParameter("sessionKey");
        String s = redis.get(sessionKey);
        WechatUser wechatUser = wechatUserService.selectByOpenId(s);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            throw new AdscmSystemException("未注册");
        }
        path= wechatUploadPath+ path.replace(wechatUploadUrl,"");
        File file =new File(path);
        Boolean result=false;
        if (file.exists()){
            result=  file.delete();
        }else {
            throw  new AdscmSystemException("该图片已经删除");
        }
        return new SimpleCoreResponse("200","上传成功",result);
    }

}

